﻿using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.DataUpdater;
using AgencyManagerSystem.Model;
using System;
using System.Linq;

namespace AgencyManagerSystem.Core
{
    public class RoleUpdaterCreator: MainDetailUpdaterCreator<RoleModel,RoleTableModel>
    {
        RoleFetcher roleFetcher = new RoleFetcher();
        RoleTableFetcher roleTableFetcher = new RoleTableFetcher();
        public RoleUpdater CreateRoleUpdaterInstance()
        {
            var mainUpdater = RoleUpdater.GetMainUpdater();
            mainUpdater.DataUpdating += FlagAndApproveFlagValid;
            mainUpdater.DataDeleting += DataReferenced;
            RoleUpdater roleUpdater = new RoleUpdater(roleFetcher, roleTableFetcher, mainUpdater);
            roleUpdater.DataBulkUpdating += CommonValid;
            roleUpdater.DataBulkUpdating += DataNotSpace; ;
            roleUpdater.DataBulkUpdating += CodeUnique;
            roleUpdater.DataBulkUpdating += ModuleExists;

            return roleUpdater;
        }

        private void DataReferenced(BaseUpdater<RoleModel> sender, DataDeletingEventArgs<RoleModel> e)
        {
            if (e.Delete == null)
                return;
            RoleUserFetcher roleUserFetcher = new RoleUserFetcher();
            var roleUsers = roleUserFetcher.ListByRole(e.Delete.AutoId);
            if(roleUsers.Count>0)
            {
                e.Message = String.Format("已经被分配到用户的档案不允许删除");
                e.Cancel = true;
                return;
            }
        }

        private void ModuleExists(MainDetailUpdater<RoleModel, RoleTableModel> sender, DataBulkUpdatingEventArgs<RoleModel, RoleTableModel> e)
        {
            if (e.Insert == null || e.DetailInsert == null)
                return;
            ModuleFetcher moduleFetcher = new ModuleFetcher();
            var moduleIds = from rtm in e.DetailInsert select rtm.Module;
            moduleIds = moduleIds.Distinct();
            Condition condition = new Condition() { Key = "AutoId", Op = "in", Value = moduleIds };
            var moduleList = moduleFetcher.List(new Condition[] { condition });
            if(moduleList.Count()!=moduleIds.Count())
            {
                e.Message = "集合中包含失效的模块ID";
                e.Cancel = true;
                return;
            }
        }

        private void CodeUnique(MainDetailUpdater<RoleModel, RoleTableModel> sender, DataBulkUpdatingEventArgs<RoleModel, RoleTableModel> e)
        {
            if (e.Insert == null)
                return;
            RoleFetcher roleFetcher = new RoleFetcher();
            RoleModel roleModel = roleFetcher.GetByCode(e.Insert.Code);
            if(roleModel!=null&&roleModel.AutoId!=e.Insert.AutoId)
            {
                e.Message = "代号已被占用";
                e.Cancel = true;
            }
        }

        private void DataNotSpace(MainDetailUpdater<RoleModel, RoleTableModel> sender, DataBulkUpdatingEventArgs<RoleModel, RoleTableModel> e)
        {
            if (e.Insert == null )
                return;
            if(String.IsNullOrWhiteSpace(e.Insert.Code)|| String.IsNullOrWhiteSpace(e.Insert.Name))
            {
                e.Message = "代号或名称不能为空";
                e.Cancel = true;
            }
        }
    }
}
